//
//  SearchViewController.m
//  Koala_Fm
//
//  Created by dllo on 16/3/3.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SearchViewController.h"
#import "AFNetBlock.h"
#import "BarModel.h"
#import "SearchTableViewCell.h"
#import <UIImageView+WebCache.h>
@interface SearchViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate, UISearchResultsUpdating>
@property(nonatomic, retain)UITableView *tableView;/**< 显示数据内容 */
@property(nonatomic, retain)UISearchController*search;/**< 搜索控制视图 */
@property(nonatomic, copy) NSString *strHight;/**< 高亮用字符串 保存搜索信息 */
@property(nonatomic, retain)NSMutableArray *dataArr;/**< 接收数据数组 */
@property(nonatomic, assign)NSInteger *number;//记录点击不更新数据 当输入搜索内容时 开始搜索

@end

@implementation SearchViewController
- (void)dealloc
{
    [_search release];
    [_strHight release];
    [_dataArr release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //初始化搜索视图
    self.search = [[UISearchController alloc]initWithSearchResultsController:nil];
    //签订协议
    _search.searchResultsUpdater = self;
    //屏幕变灰 yes时tabView 不能下拉
    _search.dimsBackgroundDuringPresentation = NO;
    //设置点击搜索框时 隐藏导航栏
    _search.hidesNavigationBarDuringPresentation = NO;
    //占位符 默认searcfdgfdgh
    _search.searchBar.placeholder = @"搜索感兴趣的内容";
    _search.searchBar.frame = CGRectMake(self.search.searchBar.frame.origin.x, self.search.searchBar.frame.origin.y, self.search.searchBar.frame.size.width, 44);
    // 搜索栏风格
    _search.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    // 把搜索视图添加到导航栏上
    self.navigationItem.titleView = _search.searchBar;
    [_search release];
    /** tableView 创建**/
    [self creatTableView];
    
    
}
#pragma mark - tableView创建
- (void)creatTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    [_tableView registerClass:[SearchTableViewCell class] forCellReuseIdentifier:@"cell"];
   
    
}


#pragma mark  - 实现搜索栏的协议方法
// 知道搜索栏有内容就执行下面的方法

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    
    // 将搜索内容转化为字符串
    // 等同于 获取搜索的信息 显示的文本
    self.strHight = [self.search.searchBar text];
    if ([_strHight containsString:@" "]) {
        // 判断如果有空格不执行
        // 没有执行以下程序
        //不写这个判断 打空格就会崩溃
    }else{
        [AFNetBlock GetDataWithURL:_string block:^(id block) {
            NSArray *array =block[@"result"][@"dataList"];
            self.dataArr = [BarModel modelHandlerWithArray:array];
            
            
        }];
        
    }
    //第一次点击不能认为是搜索
    if (_number) {
        NSString *searchStr = _search.searchBar.text;
        NSMutableArray *searchArr = [NSMutableArray array];
        
        for (BarModel *m in _dataArr) {
            if ([m.name rangeOfString:searchStr].location !=NSNotFound) {
                [searchArr addObject:m];
            }
        }
        [_dataArr removeAllObjects];
        
        for (BarModel * m in searchArr) {
            [_dataArr addObject:m];
        }
        //判断没有搜索到内容时显示
//        if (![_search.searchBar.text isEqualToString:@" "] && _dataArr.count == 0) {
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"您要搜索的内容为空" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
//            [alert show];
//        }

        [_tableView reloadData];
    }
    
    _number++;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}
// 赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    BarModel *m = _dataArr[indexPath.row];
   [cell.imag sd_setImageWithURL:[NSURL URLWithString:m.pic]];
    //高亮设置
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:m.name];
    //取范围
    NSRange range = [m.name rangeOfString:_strHight];
    [attStr setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName: [UIFont systemFontOfSize:20]} range:range];
    cell.lable.attributedText = attStr;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
